package epimarket.beans;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import javax.faces.component.html.HtmlInputText;
import javax.faces.model.ListDataModel;
import epimarket.beans.DataManager;
import epimarket.*;

public class ShopManager {
  private Product       product;
  private List          products;
  private ListDataModel productsDataModel = new ListDataModel();
  private ListDataModel cartItems = new ListDataModel();
  private ListDataModel categoriesDataModel = new ListDataModel();
  private String        categoryName;
  private String		categoryPicture;
  private Customer      customer;
  private DataManager   dataManager = new DataManager();
  private long          orderId;
  private double        orderTotal;
  private HtmlInputText searchTxt = new HtmlInputText();
  private Hashtable<String, CartItem>
                        shoppingCart = new Hashtable<String, CartItem>();

  public String addToCart() {
    CartItem item = shoppingCart.get(product.getId());
    if (item == null) {
      item = new CartItem(product, 1);
      shoppingCart.put(product.getId(), item);
      }
    else {
      item.setQuantity(item.getQuantity() + 1);
      }
    return "showCart";
    }

  public String checkOut() {
    orderId = dataManager.insertOrder(customer, shoppingCart);
    if (orderId != 0) {
      customer = null;
      shoppingCart.clear();
      }
    return "orderConfirmation";
    }

  public String deleteItem() {
    CartItem item = (CartItem)cartItems.getRowData();
    shoppingCart.remove(item.getProductID());
    return null;
    }

  public Product getProduct() {
    return product;
    }

  public ListDataModel getProducts() {
    if (products == null) {
      productsDataModel.setWrappedData(new LinkedList<Product>());
      }
    else {
      productsDataModel.setWrappedData(products);
      }
    return productsDataModel;
    }

  public ListDataModel getCategories() {
    categoriesDataModel.setWrappedData(dataManager.getCategories());
    return categoriesDataModel;
    }

  public String getCategoryName() {
    return categoryName;
    }
  
  public String	getCategoryPicture() {
	return categoryPicture;
  }
  
  public String getContactName() {
    return getCustomer().getContactName();
    }

  public Customer getCustomer() {
    if (customer == null) {
      customer = new Customer();
      }
    return customer;
    }

  public String getOrderId() {
    if (orderId == 0) return "";
    return Long.toHexString(orderId).toUpperCase();
    }

  public double getOrderTotal() {
    orderTotal = 0.;
    Enumeration<CartItem> items = shoppingCart.elements();
    while (items.hasMoreElements()) {
      orderTotal += items.nextElement().getSubtotal();
      }
    orderTotal = Math.round(orderTotal*100.)/100.;
    return orderTotal;
    }

  public HtmlInputText getSearchTxt() {
    return searchTxt;
    }

  public ListDataModel getShoppingCart() {
    ArrayList<CartItem> itemList =
        new ArrayList<CartItem>(shoppingCart.values());
    cartItems.setWrappedData(itemList);
    return cartItems;
    }

  public boolean isShoppingCartEmpty() {
    return shoppingCart.isEmpty();
    }

  public String searchProducts() {
    categoryName=null;
    String searchKeyword = (String)searchTxt.getSubmittedValue();
    products = dataManager.getSearchResults(searchKeyword);
    return "listProducts";
    }

  public String selectProduct() {
    product = (Product)productsDataModel.getRowData();
    return "showProduct";
    }

  public String selectCategory() {
    Category category = (Category)categoriesDataModel.getRowData();
    categoryName = category.getName();
	categoryPicture = category.getPicture();
    products = dataManager.getProductsInCategory(Integer.toString(category.getId()));
    return "listProducts";
    }

  public void setContactName(String contactName) {
    getCustomer().setContactName(contactName);
    }

  public void setCustomer(Customer order) {
    this.customer = order;
    }

  public void setSearchTxt(HtmlInputText val) {
    searchTxt = val;
    }

  public String updateItem() {
    return null;
    }
  }
